package com.liveinstars.im.api.impl;

import com.liveinstars.im.common.enums.PassThroughMessageTypeEnum;
import com.liveinstars.im.core.SessionManager;
import com.liveinstars.im.core.service.MessageService;
import com.liveinstars.im.domain.dao.model.ChatGroupMember;
import com.liveinstars.im.domain.dao.model.Todo;
import com.liveinstars.im.domain.manager.ChatGroupMemberManager;
import com.liveinstars.im.entity.ChatMessage;
import com.liveinstars.im.entity.Msg;
import com.liveinstars.im.api.MsgGeneratorService;
import com.liveinstars.im.api.NotifyData;
import com.liveinstars.im.corebean.MsgHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.UUID;

@Service
public class MsgGeneratorServiceImpl implements MsgGeneratorService {

    private static final Logger LOGGER = LoggerFactory.getLogger(MsgGeneratorServiceImpl.class);


    @Autowired
    private MessageService messageService;

    @Autowired
    private ChatGroupMemberManager chatGroupMemberManager;

    @Autowired
    SessionManager sessionManager;

    @Override
    public void sendNotifyMessage(NotifyData notifyData) {
        int msgType;
        if(notifyData.getChatGroupId() == null || notifyData.getChatGroupId() == 0){
            msgType = ChatMessage.MSG_TYPE_UU;
        }else {
            msgType = ChatMessage.MSG_TYPE_UCG;
        }
        ChatMessage chatMessage = new ChatMessage(notifyData.getUserId(), notifyData.getToId(), UUID.randomUUID().toString()
                , ChatMessage.STATUS_SEND, msgType, notifyData.getContent()
                , true, (int) LocalDateTime.now().atZone(ZoneId.systemDefault()).toEpochSecond(), 2, notifyData.getChatGroupId());
        chatMessage.setType(ChatMessage.TYPE_SEND);
        chatMessage.setId(messageService.getMsgId());


        if(notifyData.getChatGroupId() != 0 && !notifyData.isOnlyOne()) {
            List<ChatGroupMember> members = chatGroupMemberManager.listMemberByChatGroupId((long)notifyData.getChatGroupId());
            chatMessage = messageService.saveChatMessage(chatMessage, true, members);
            for (ChatGroupMember member : members) {
                chatMessage.setToId(member.getUserId());
                Msg.Message message = MsgHelper.newChatMessage(chatMessage);
                sessionManager.sendMessageRoaming(chatMessage.getToId(),
                        PassThroughMessageTypeEnum.USER.getValue(),
                        message);
            }
        }else {
            Msg.Message message = MsgHelper.newChatMessage(chatMessage);
            sessionManager.sendMessageRoaming(chatMessage.getToId(),
                    PassThroughMessageTypeEnum.USER.getValue(),
                    message);
        }
    }

    @Override
    public void sendTodoMessage(Todo todo, Long toId) {
        Msg.Message msg = MsgHelper.newTodoMessage(todo);
        LOGGER.info("MsgGeneratorServiceImpl sendMessageRoaming todo");
        sessionManager.sendMessageRoaming(toId, PassThroughMessageTypeEnum.USER.getValue(), msg);
    }

    @Override
    public void sendRoamingMessage(Long toId, PassThroughMessageTypeEnum passThroughMessageTypeEnum, Msg.Message message) {
        sessionManager.sendMessageRoaming(toId, passThroughMessageTypeEnum.getValue(), message);
    }
}
